package com.ynet.hmds28.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.taobao.pandora.boot.loader.util.StringUtils;
import com.ynet.hm.user.service.QueryConsumeByUserIdService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hmds28.user.dao.ServiceconsumeDao;
import com.ynet.hmds28.user.dao.ServiceprovideDao;
import com.ynet.hmds28.user.model.Serviceconsume;
import com.ynet.hmds28.user.model.ServiceconsumeQuery;
import com.ynet.hmds28.user.model.Serviceprovide;
import com.ynet.hmds28.user.model.extend.FieldClass;
import com.ynet.hmds28.user.model.extend.ServiceConsumeExtend;
@Transactional
@HSFProvider(serviceInterface = QueryConsumeByUserIdService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class QueryConsumeByUserIdServiceImpl implements
		QueryConsumeByUserIdService {
	@Autowired	
	private ServiceconsumeDao serviceconsumeDao;
	@Autowired
	private ServiceprovideDao serviceprovideDao;
	@Override
	public Result<?> queryConsumeByUserId(String userId,String pageNo,String pageSize) {
		ServiceconsumeQuery ssq=new ServiceconsumeQuery();
		ssq.createCriteria().andUseridEqualTo(userId);
		int totalNum=serviceconsumeDao.countByExample(ssq);
		if(StringUtils.isEmpty(pageNo)){
			pageNo="1";
		}
		if(StringUtils.isEmpty(pageSize)){
			pageSize="10";
		}
		ssq.setPageNo(Integer.parseInt(pageNo));
		ssq.setPageSize(Integer.parseInt(pageSize));
		ssq.setStartRow((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
		List<Serviceconsume> sss=serviceconsumeDao.selectByExample(ssq);
		List<ServiceConsumeExtend> sces=new ArrayList<>();
		if(!CollectionUtils.isEmpty(sss)){
			for(Serviceconsume scs:sss){
				ServiceConsumeExtend sce=new ServiceConsumeExtend();
				sce.setCallcost(scs.getCallcost());
				sce.setCallnum(scs.getCallnum());
				sce.setServiceconsumeid(scs.getServiceconsumeid());
				sce.setSeviceproviceid(scs.getSeviceproviceid());
				sce.setUserid(scs.getUserid());
				String serviceProviceId=scs.getSeviceproviceid();
				Serviceprovide sp=serviceprovideDao.selectByPrimaryKey(serviceProviceId);
				sce.setServiceName(sp.getServicename());
				sce.setServiceDesc(sp.getServicedesc());
				//查询输入字段
				List<FieldClass> importString=new ArrayList<>();
				String importField=sp.getImportfield();
				if(!StringUtils.isEmpty(importField)){
					String[] strs=importField.split(",");
					for(int i=0;i<strs.length;i++){
						FieldClass fc=new FieldClass();
						String[] str=strs[i].split("-");
						fc.setEnglishName(str[0]);
						fc.setChineseName(str[1]);
						importString.add(fc);
					}
				}
				sce.setImportField(importString);
				//查询输出字段
				List<FieldClass> exportString=new ArrayList<>();
				String exportField=sp.getExportfield();
				if(!StringUtils.isEmpty(exportField)){
					String[] strs=exportField.split(",");
					for(int i=0;i<strs.length;i++){
						FieldClass fc=new FieldClass();
						String[] str=strs[i].split("-");
						fc.setEnglishName(str[0]);
						fc.setChineseName(str[1]);
						exportString.add(fc);
					}
				}
				sce.setExportField(exportString);
				sces.add(sce);
			}
		}
		Map<String,Object> m=new HashMap<>();
		m.put("totalNum", totalNum);
		m.put("serviceConsume", sces);
		return ResultUtil.success(m);
	}

}
